package com.shelpe.services.sinbad.entity.jpa;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.Data;

@Entity(name = "task_progress")
@Data
public class TaskProgress {

	@Id
	@Column(name = "task_id")
	private String taskID;
	private String taskName;
	@Column(name = "user_id")
	private long userID;
	private long processedNum;
	private long totalNum;
	@Convert(converter = StatusConverter.class)
	private Status status;
	private Date startTime;
	private Date endTime;
	private String message;
	private Date updateTime;
	@Column(name = "sem_id")
	private long semID;
	@Column(name = "container_id")
	private long containerID;
	private String containerType;
	private int isNeedSync; // 是否需要同步。0不需要 1需要
	@Column(name = "product_id")
	private long productID;

	public static enum Status {
		Running("Running"), Failed("Failed"), Completed("Completed"), Timeout("Timeout");

		private String value;

		private Status(String v) {
			this.value = v;
		}

		public String getValue() {
			return this.value;
		}

		public static Status from(String value) {
			Status en = Status.Running;
			for (Status s : Status.values()) {
				if (s.getValue().equals(value)) {
					en = s;
					break;
				}
			}
			return en;
		}
	}

	public static class StatusConverter implements AttributeConverter<Status, String> {
		@Override
		public String convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.Running.getValue();
		}

		@Override
		public Status convertToEntityAttribute(String dbData) {
			return Status.from(dbData);
		}
	}
}
